package com.sgb.capital.view.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sgb.capital.R;
import com.sgb.capital.base.BaseActivity;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.ZMessageEvent;
import com.sgb.capital.databinding.ReimburseActivityBinding;
import com.sgb.capital.model.Bean;
import com.sgb.capital.model.ReimburseBean;
import com.sgb.capital.utils.AdapterOnItemClick;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.pop.SelectPop;
import com.sgb.capital.view.ui.adapter.FinanceFlowItemAdapter;
import com.sgb.capital.view.ui.adapter.ReimburseAdapter;
import com.sgb.capital.view.widget.xrecyclerview.ZXRecyclerView;
import com.sgb.capital.viewmodel.ReimburseModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

/**
 * 作者:张磊
 * 日期:2021/12/15 0015
 * 说明:退款列表
 */
public class ReimburseActivity extends BaseActivity {
    ReimburseActivityBinding mBinding;
    ReimburseModel mModel;
    private FinanceFlowItemAdapter mFlowItemAdapter;
    private ReimburseAdapter mAdapter;
    private List<Bean> mApplyTime;
    private List<Bean> mHandleTime;
    private List<Bean> mStatus;
    private TextView mTextView;
    private ImageView mImg;
    private SelectPop mSelectPop;
    private int mIndex = 1;
    private HashMap mMap;

    public static void start(Context context) {
        Intent intent = new Intent(context, ReimburseActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        Utils.setStatusBarLightMode(this, true);
        EventBus.getDefault().register(this);
        mBinding = DataBindingUtil.setContentView(this, R.layout.reimburse_activity);
    }

    @Override
    public void initData() {
        mModel = new ViewModelProvider(this).get(ReimburseModel.class);
        // TODO: 2021/4/20 0020 选项
        mBinding.rvItem.setLayoutManager(new GridLayoutManager(Utils.getContext(), 3));
        mFlowItemAdapter = new FinanceFlowItemAdapter(null);
        mBinding.rvItem.setAdapter(mFlowItemAdapter);
        // TODO: 2021/4/20 0020 列表
        mAdapter = new ReimburseAdapter(this, null);
        mBinding.rvList.setLayoutManager(new GridLayoutManager(Utils.getContext(), 1));
        mBinding.rvList.setAdapter(mAdapter);

        mMap = new HashMap();
        mMap.put("limit", 20);
        mMap.put("page", mIndex);
        mSelectPop = new SelectPop(this);
        mSelectPop.setBgView(mBinding.grayLayout);
        mApplyTime = Utils.setDatas(new String[]{"全部", "今日", "最近一周", "最近一个月", "最近三个月", "最近六个月", "最近一年", "最近三年"});
        mHandleTime = Utils.setDatas(new String[]{"全部", "今日", "最近一周", "最近一个月", "最近三个月", "最近六个月", "最近一年", "最近三年"});
        mStatus = Utils.setDatas(new String[]{"全部", "待处理", "已拒绝", "退款中", "已完成", "已失败"});

        List<Bean> beans = new ArrayList<>();
        beans.add(new Bean("申请时间"));
        beans.add(new Bean("处理时间"));
        beans.add(new Bean("退款状态"));
        mFlowItemAdapter.setNewInstance(beans);
        mMap.put("page", mIndex);
        mMap.put("searchValue", mBinding.etSs.getText().toString().trim());
        mModel.refundPageList(this, mMap);
    }

    @Override
    public void initListener() {
        // TODO: 2021/12/17 0017  
        mBinding.rvList.setLoadingListener(new ZXRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mIndex = 1;
                mMap.put("page", mIndex);
                mMap.put("searchValue", mBinding.etSs.getText().toString().trim());
                mModel.refundPageList(ReimburseActivity.this, mMap);
            }

            @Override
            public void onLoadMore() {
                mIndex++;
                mMap.put("page", mIndex);
                mMap.put("searchValue", mBinding.etSs.getText().toString().trim());
                mModel.refundPageList(ReimburseActivity.this, mMap);
            }
        });
        mAdapter.setOnItemClick(new AdapterOnItemClick<ReimburseBean.ListDTO>() {
            @Override
            public void onItemClick(ReimburseBean.ListDTO listDTO, int position) {
                ReimburseDetailsActivity.start(ReimburseActivity.this, new Gson().toJson(listDTO));
            }
        });

        mBinding.etSs.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mMap.put("page", mIndex);
                mMap.put("searchValue", mBinding.etSs.getText().toString().trim());
                mModel.refundPageList(this, mMap);
            }
            return false;
        });
        mBinding.ivBack.setOnClickListener(v -> finish());


        mSelectPop.setOnDismissListener(() -> {
            if (mSelectPop.mType == 1 && mSelectPop.mPos1 == 0) {
                mTextView.setText("申请时间");
                mMap.put("applyEndTime", null);
                mMap.put("applyStartTime", null);
            } else if (mSelectPop.mType == 2 && mSelectPop.mPos2 == 0) {
                mTextView.setText("处理时间");
                mMap.put("handingEndTime", null);
                mMap.put("handingStartTime", null);
            } else if (mSelectPop.mType == 3 && mSelectPop.mPos3 == 0) {
                mTextView.setText("退款状态");
                mMap.put("status", null);
            } else {
                if (mSelectPop.mIsClick) {
                    mTextView.setText(mSelectPop.mBean.name.contains("B2B") ? mSelectPop.mBean.name : mSelectPop.mBean.name.length() > 5 ? mSelectPop.mBean.name.substring(0, 5) + "..." : mSelectPop.mBean.name);
                    if (mSelectPop.mType == 1) {
                        String time = mSelectPop.mBean.name.contains("今日") ? Utils.getStartAndEndTime(0, 0, "今日")
                                : mSelectPop.mBean.name.contains("最近一周") ? Utils.getStartAndEndTime(1, 7, "最近一周")
                                : mSelectPop.mBean.name.contains("最近一个月") ? Utils.getStartAndEndTime(2, 1, "最近一个月")
                                : mSelectPop.mBean.name.contains("最近三个月") ? Utils.getStartAndEndTime(2, 3, "最近三个月")
                                : mSelectPop.mBean.name.contains("最近六个月") ? Utils.getStartAndEndTime(2, 6, "最近六个月")
                                : mSelectPop.mBean.name.contains("最近一年") ? Utils.getStartAndEndTime(3, 1, "最近一年")
                                : Utils.getStartAndEndTime(3, 3, "最近三年");
                        mMap.put("applyEndTime", time.split("-")[1]);
                        mMap.put("applyStartTime", time.split("-")[0]);
                    } else if (mSelectPop.mType == 2) {
                        String time = mSelectPop.mBean.name.contains("今日") ? Utils.getStartAndEndTime(0, 0, "今日")
                                : mSelectPop.mBean.name.contains("最近一周") ? Utils.getStartAndEndTime(1, 7, "最近一周")
                                : mSelectPop.mBean.name.contains("最近一个月") ? Utils.getStartAndEndTime(2, 1, "最近一个月")
                                : mSelectPop.mBean.name.contains("最近三个月") ? Utils.getStartAndEndTime(2, 3, "最近三个月")
                                : mSelectPop.mBean.name.contains("最近六个月") ? Utils.getStartAndEndTime(2, 6, "最近六个月")
                                : mSelectPop.mBean.name.contains("最近一年") ? Utils.getStartAndEndTime(3, 1, "最近一年")
                                : Utils.getStartAndEndTime(3, 3, "最近三年");
                        mMap.put("handingEndTime", time.split("-")[1]);
                        mMap.put("handingStartTime", time.split("-")[0]);
                    } else if (mSelectPop.mType == 3) {
                        //  0.待处理,1.退款中,2退费成功,3退费失败 6.已拒绝)   {"全部", "待处理", "已拒绝", "退款中", "已完成", "已失败"};
                        mMap.put("status", mSelectPop.mPos3 == 0 ? null :
                                mSelectPop.mPos3 == 1 ? 0 :
                                        mSelectPop.mPos3 == 2 ? 6 :
                                                mSelectPop.mPos3 == 3 ? 1 :
                                                        mSelectPop.mPos3 == 4 ? 2 : 3);
                    }
                }
            }
            mImg.setRotation(0);
            if (mSelectPop.mIsClick) {
                mTextView.setTextColor(Utils.getColor(
                        mSelectPop.mType == 1 ? mSelectPop.mPos1 != 0 ? R.color.color_EF4033 : R.color.color_666666 :
                                mSelectPop.mType == 2 ? mSelectPop.mPos2 != 0 ? R.color.color_EF4033 : R.color.color_666666 :
                                        mSelectPop.mPos3 != 0 ? R.color.color_EF4033 : R.color.color_666666
                ));
                mImg.setImageResource(mSelectPop.mType == 1 ? mSelectPop.mPos1 != 0 ?
                        R.mipmap.down : R.mipmap.ic_down_arrow_n : mSelectPop.mType == 2 ?
                        mSelectPop.mPos2 != 0 ? R.mipmap.down : R.mipmap.ic_down_arrow_n :
                        mSelectPop.mPos3 != 0 ? R.mipmap.down : R.mipmap.ic_down_arrow_n
                );
                mIndex = 1;
                mMap.put("page", mIndex);
                mMap.put("searchValue", mBinding.etSs.getText().toString().trim());
                mModel.refundPageList(this, mMap);
            }
            mSelectPop.mIsClick = false;
        });

        mFlowItemAdapter.setOnItemClickListener((adapter, view, position) -> {
            LinearLayout linearLayout = (LinearLayout) view;
            mTextView = (TextView) linearLayout.getChildAt(0);
            mImg = (ImageView) linearLayout.getChildAt(1);
            mImg.setRotation(180);
            switch (position) {
                case 0:
                    mSelectPop.mType = 1;
                    mSelectPop.setData(mApplyTime);
                    mSelectPop.mAdapter.setDefItem(mSelectPop.mPos1);
                    break;
                case 1:
                    mSelectPop.mType = 2;
                    mSelectPop.setData(mHandleTime);
                    mSelectPop.mAdapter.setDefItem(mSelectPop.mPos2);
                    break;
                case 2:
                    mSelectPop.mType = 3;
                    mSelectPop.setData(mStatus);
                    mSelectPop.mAdapter.setDefItem(mSelectPop.mPos3);
                    break;
            }
            mSelectPop.showPopwindow(mBinding.view, 0, 0);
        });

    }

    @Override
    public void initObserve() {
        mModel.mDatas.observe(this, data -> {
            mBinding.rvList.refreshComplete();
            if (data == null) {
                return;
            }
            if (mIndex == 1) {
                mBinding.errorView.setVisibility(data.size() != 0 ? View.GONE : View.VISIBLE);
                mBinding.rvList.setLoadingMoreEnabled(true);
                mBinding.rvList.refreshComplete();
                mAdapter.setDatas(data);
            } else {
                if (data.size() != 0) {
                    mBinding.rvList.loadMoreComplete();
                    mAdapter.addDatas(data);
                } else {
                    mBinding.rvList.setNoMore(true);
                }
            }
            mAdapter.notifyDataSetChanged();
        });
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void getCode(ZMessageEvent event) {
        switch (event.code) {
            case Constants
                    .EVENT_REFRESH:
                mIndex = 1;
                mMap.put("page", mIndex);
                mMap.put("searchValue", mBinding.etSs.getText().toString().trim());
                mModel.refundPageList(this, mMap);
                break;
        }
    }
}